package Smallprogram.serlet;

import Admin.entity.Admin;
import Admin.service.impl.AdminServiceImpl;
import Smallprogram.entiy.Orderbiao;
import Smallprogram.entiy.Users;
import Smallprogram.servict.impl.OrderbiaoServictImpl;
import Smallprogram.servict.impl.UsersServletImpl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import javax.swing.text.html.HTMLWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;

@WebServlet(name = "InsertOrderbiaoServlet", value = "/InsertOrderbiaoServlet")
public class InsertOrderbiaoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String currentTimeid = request.getParameter("currentTimeid");//下单时间
        String totalprice = request.getParameter("totalprice");//总金额
        String amountPrice = request.getParameter("amountPrice");//金额
        String cart = request.getParameter("cart");//菜品数组
        String users = request.getParameter("users");//用户数组
        String orderType = request.getParameter("orderType");//判断是自取(takein)还是外卖(takeout)
        String form = request.getParameter("form");
        System.out.println(users);
        JSONArray tempcart = JSONArray.parseArray(cart);
        JSONArray tempusers = JSONArray.parseArray(users);

        PrintWriter out = response.getWriter();

        String uuid = UUID.randomUUID().toString().replace("-", "");//生成随机数
        Orderbiao.Goods goods = null;
        JSONObject jsonObject = tempusers.getJSONObject(0);
        Users use = new Users();//用户
        UsersServletImpl useSer = new UsersServletImpl();
        Admin adm = new Admin();
        AdminServiceImpl admin = new AdminServiceImpl();
        double totalpriceitem = totalprice != null ? Double.parseDouble(totalprice) : 0;
        if (jsonObject.size() > 0) {
            String usersid = jsonObject.getString("usersid");
            String account = jsonObject.getString("account");
            String balance = jsonObject.getString("balance");
            String address = jsonObject.getString("address");
            String eaddress = jsonObject.getString("eaddress");
            String password = jsonObject.getString("password");
            String telephone = jsonObject.getString("telephone");
            String userpoints = jsonObject.getString("userpoints");
            int userpointsime = userpoints != null ? Integer.parseInt(userpoints) + (int) totalpriceitem / 10 : 0;
            double balanceime = balance != null ? Double.parseDouble(balance) - totalpriceitem : 0;
            use.setUserpoints(userpointsime);
            use.setUsersid(usersid != null ? Integer.parseInt(usersid) : 0);
            use.setAccount(account);
            use.setBalance(balanceime);
            use.setAddress(address);
            use.setEaddress(eaddress);
            use.setPassword(password);
            use.setTelephone(telephone);
            useSer.updateysers(use);
            double dine = admin.selectDalance();
            adm.setBalance(dine + totalpriceitem);
            admin.updatedalance(adm);
        }

        OrderbiaoServictImpl serOed = new OrderbiaoServictImpl();
        int ien = serOed.selectMAXOrder() + 1;
        for (int i = 0; i < tempcart.size(); i++) {
//            Orderbiao.Goods
            jsonObject = tempcart.getJSONObject(i);
            String id = jsonObject.getString("id");
            String name = jsonObject.getString("name");
            String price = jsonObject.getString("price");
            String number = jsonObject.getString("number");
            String originAmount = jsonObject.getString("originAmount");
            goods = new Orderbiao.Goods();//订单详情
            goods.setSnacksid(id != null ? Integer.parseInt(id) : 0);
            goods.setName(name);
            double imen = price != null ? Double.parseDouble(price) : 0.00;
            goods.setPrice(imen);
            goods.setAmount(imen);
            goods.setOrderbiaoid(ien);
            goods.setNumber(number != null ? Integer.parseInt(number) : 0);
            goods.setOriginAmount(originAmount != null ? Double.parseDouble(originAmount) : 0.00);
            serOed.insertDetailsdb(goods);
        }
        Orderbiao ord = new Orderbiao();
        ord.setCompleted_time(currentTimeid);
        ord.setOrder_no(uuid);
        ord.setUserid(use.getUsersid());
        ord.setPostscript(form != null ? form : null);
        if (orderType.equals("takein")) {
            ord.setHelpuourslf(1);
        } else {
            ord.setHelpuourslf(0);
        }
        int i = serOed.insertOrderbiao(ord);
//        out.print(JSON.toJSONString(i));

        ServletContext context = getServletContext();
        String idd = String.valueOf(ien);
        context.setAttribute("id", idd);


        if (i>0) {
            OrderbiaoServictImpl Ord = new OrderbiaoServictImpl();
            List<Orderbiao> list = Ord.selectorderbiaoid(ien);
            String listnem = JSON.toJSONString(list);

        if (list!=null){
            out.print(listnem);
        }
        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
